<%-- 
    Document   : index
    Created on : 13.03.2011, 11:45:16
    Author     : Alejandro
--%>



<%@page import="java.io.Writer"%>
<%@page import="java.io.StringWriter"%>
<%@page import="java.io.Reader"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.net.URL"%>
<%@page import="java.io.File"%>
<%@page import="wrappers.AbstractWrap"%>
<%@page import="wrappers.DepartmentWrap"%>
<%@page import="wrappers.EmployeeWrap"%>
<%@page import="wrappers.CountryWrap"%>
<%@page import="strategies.IStrategy"%>
<%@page import="general.OutputProvider"%>
<%@page import="java.util.Set"%>
<%@page import="web.Context"%>
<%@page import="web.Linker"%>
<%@page import="web.HtmlOutput"%>
<%@page import="web.ParamStorage"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<%! boolean isAscending = true;
    int initialSelection = 0;
    boolean showSearchbar = false;
    Linker mp = Linker.getInstance();
%>

<%
    ParamStorage pp = new ParamStorage(request);
    OutputProvider op = new HtmlOutput(out);
    initialSelection = 0;

    if (pp.hasParam("table-selection"))
        initialSelection = Integer.parseInt(pp.getParam("table-selection"));

    mp.setMessageMap(request);
    if (pp.hasParam("view_employees")) {
        initialSelection = 1;
        showSearchbar = true;
    } else if (pp.hasParam("view_departments")) {
        initialSelection = 2;
        showSearchbar = false;
    } else if (pp.hasParam("view_countries")) {
        initialSelection = 3;
        showSearchbar = false;
    }
    if (pp.hasParam("table-selection")) {
        if (initialSelection != Integer.parseInt(pp.getParam("table-selection")))
            pp.setParam("table-selection", Integer.toString(initialSelection));
    }
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" type="text/css" href="style.css" />
        <title>Database main page</title>
        <script type="text/javascript" src="lib.js"></script>
    </head>
    <body>
        <div>
            <div id="message" style="visibility: hidden; position: absolute;"></div>
            <h1>Database Admin Page</h1>
            <form id="main" method="post" action="index.jsp" >
                <div id="control">
                    <input type="hidden" name="table-selection" id="table-selection" value="<% out.write(Integer.toString(initialSelection)); %>" />
                    <a href="help.html">Help</a>
                    <input type="submit" name="view_employees" id="view_employees" value="Employees" onclick="onChangedTable(1); " class=<% out.write("\""+((initialSelection == 1) ? "selected_tab":"tab")+"\""); %> />
                    <input type="submit" name="view_departments" id="view_departments" value="Departments" onclick="onChangedTable(2); " class=<% out.write("\""+((initialSelection == 2) ? "selected_tab":"tab")+"\""); %> />
                    <input type="submit" name="view_countries" id="view_countries" value="Countries" onclick="onChangedTable(3); " class=<% out.write("\""+((initialSelection == 3) ? "selected_tab":"tab")+"\""); %> />
                    <input type="submit" name="new" id="new" value="New" class="submit" />
                    <input type="submit" name="edit" id="edit" value="Edit" class="submit" />
                    <input type="submit" name="moveto" id="moveto" value="Move to department" class="submit" />
                    <input type="submit" name="delete" id="delete" value="Delete" class="submit" />
                    <%
                        if (showSearchbar) {
                            %>
                            <br />
                            <label for="searchbar" id="searchbar_label">Search employee</label>
                            <%
                            String searchQuery = "";
                            if ((pp.hasParam("search") || (pp.hasParam("search2") && !pp.getParam("search2").isEmpty())) && pp.hasParam("searchbar"))
                                searchQuery = pp.getParam("searchbar");
                            %>
                            <input type="text" name="searchbar" id="searchbar" class="text" value="<% out.write(searchQuery); %>" onkeypress="return submitEnter(this, event);" />
                            <input type="hidden" value="" id="search2" name="search2" />
                            <input type="submit" value="Go" name="search" id="search" class="submit" />
                            <%
                        }
                    %>
                </div>
                <noscript>
                    <div class="exclamation">
                        Exclamation: Not all functions will be available. It is highly recommended that you enable Javascript for this page.
                    </div>
                </noscript>
                <%

                    if ( (pp.hasParam("new")) || (pp.hasParam("edit"))||(pp.hasParam("moveto")) ) {
                        getServletConfig().getServletContext().getRequestDispatcher("/edit.jsp").forward(request, response);
                    }
                    
                    Context ctx = new Context(op, pp);
                    Set<Integer> messages = mp.getMessageMap().keySet();
                    for(Integer i: messages) {
                        mp.getMessageMap().get(i).executeOperation(ctx);
                    }
                    /*
                    URL url = getServletContext().getResource("/test.txt");
                    InputStream is = (InputStream) url.getContent();
                    Writer writer = new StringWriter();
                    char[] buffer = new char[1024];
                    try {
                        Reader reader = new BufferedReader(
                        new InputStreamReader(is, "UTF-8"));
                        int n;
                        while ((n = reader.read(buffer)) != -1) {
                            writer.write(buffer, 0, n);
                        }
                    } finally {
                        is.close();
                    }
                    op.echo(writer.toString());
                    */
                %>
            </form>
            <script type="text/javascript">
                checkRowSelection();
                if (parseInt(document.getElementById('table-selection').value) == 0)
                    document.getElementById("new").style.display = 'none';
            </script>
        </div>
    </body>
</html>
